package com.gzcc.controller;

import com.gzcc.model.Category;
import com.gzcc.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 描述: 商品分类
 *
 * @Author: Mr.Zhong
 * @Date: 2018/6/13
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryRepository categoryRepository;

    /**
     * 描述: 商品分类页面
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping()
    public ModelAndView shopList() {
        ModelAndView mv = new ModelAndView();
        List<Category> category = categoryRepository.findAll();
        mv.addObject("category", category);
        mv.setViewName("backend/category/category_list.btl");
        return mv;
    }

    /**
     * 描述: 跳转增加商品分类页面
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("backend/category/category.btl");
        return mv;
    }

    /**
     * 描述:  增加商品分类
     *
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String detail(@ModelAttribute Category category, @RequestParam(value = "title") String title, @RequestParam(value = "file") MultipartFile file) {
        try {
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File("E:\\javademo\\javademo\\src\\main\\webapp\\" + title + ".jpg")));//保存图片到目录下
            out.write(file.getBytes());
            out.flush();
            out.close();
            String filename = title + ".jpg";
            category.setImage(filename);
        } catch (IOException e) {
            e.printStackTrace();
        }

        category.setTitle(title);
        Date date = new Date();
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String nowtime = simpleDateFormat.format(date);
        try {
            java.util.Date time = simpleDateFormat.parse(nowtime);
            category.setCreatetime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        categoryRepository.save(category);

        return "redirect:/category";
    }

    /**
     * 描述: 编辑商品分类
     *
     * @param id
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/toEdit", method = RequestMethod.GET)
    public ModelAndView edit(@RequestParam("id") int id) {
        ModelAndView mv = new ModelAndView();
        Category category = categoryRepository.findById(id).get();
        mv.addObject("category", category);
        mv.setViewName("backend/category/toEditCategory.btl");
        return mv;
    }

    /**
     * 描述: 删除商品分类
     *
     * @param
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam("id") long id) {
        categoryRepository.deleteById((int) id);
        return "redirect:/category";
    }

    /**
     * 描述: 查询商品分类
     *
     * @param
     * @return
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView seaech(@RequestParam("title") String title) {
        List<Category> category = categoryRepository.findAllByCategoryNameLike(title);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("category", category);
        modelAndView.setViewName("backend/category/category_search_list.btl");
        return modelAndView;

    }
}
